//
//  JWCinemaCell.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/18.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "JWCinemaCell.h"
#import "JWCinemaModel.h"
#import "constant.h"

CGFloat JWMarkWith = 20;

@interface JWCinemaCell ()
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *address;
@property (nonatomic, strong) UILabel *grade;
@property (nonatomic, strong) UILabel *lowPrice;
@property (nonatomic, strong) UILabel *distance;
@property (nonatomic, strong) UIImageView *seatMark;
@property (nonatomic, strong) UIImageView *couponMark;
@property (nonatomic, strong) UIImageView *groupMark;

@end

@implementation JWCinemaCell


- (void)awakeFromNib {
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        //      影院名
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        nameLabel.font = [UIFont systemFontOfSize:17];
        [self.contentView addSubview:nameLabel];
        _name = nameLabel;
        _name.textColor = [UIColor whiteColor];
//        _name.backgroundColor = [UIColor grayColor];
        
        //      影院地址
        
        UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        addressLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:addressLabel];
        _address = addressLabel;
        _address.textColor = [UIColor whiteColor];
//        _address.backgroundColor = [UIColor grayColor];
        
        //      电影评分
        
        UILabel *gradeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        gradeLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:gradeLabel];
        _grade = gradeLabel;
        _grade.textColor = [UIColor orangeColor];
//        _grade.backgroundColor = [UIColor grayColor];
        
        //      电影票价
        
        UILabel *lowPriceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        lowPriceLabel.font = [UIFont systemFontOfSize:20];
        [self.contentView addSubview:lowPriceLabel];
        _lowPrice = lowPriceLabel;
        _lowPrice.textColor = [UIColor orangeColor];
//        _lowPrice.backgroundColor = [UIColor grayColor];
        
        //      影院距离
        
        UILabel *distanceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        distanceLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:distanceLabel];
        _distance = distanceLabel;
        _distance.textColor = [UIColor orangeColor];
//        _distance.backgroundColor = [UIColor grayColor];
        
        //      团，券，座图标
        
        UIImageView *seatMark = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:seatMark];
        _seatMark = seatMark;
        
        UIImageView *couponMark = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:couponMark];
        _couponMark = couponMark;
        
        UIImageView *groupMark = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:groupMark];
        _groupMark = groupMark;
        
        
      
    }
    return self;
}

- (void)setCinemaModel:(JWCinemaModel *)cinemaModel{
    
    _cinemaModel = cinemaModel;
    
    //      电影名
    
    self.name.text = _cinemaModel.name;
    
    //      影院地址
    
    self.address.text = _cinemaModel.address;
    
    //      电影评分
    
    NSNumber *grade = _cinemaModel.grade;
    self.grade.text = [NSString stringWithFormat:@"%0.1f分",[grade floatValue]];
    
    //      电影票价
    NSNumber *price = _cinemaModel.lowPrice;
    self.lowPrice.text = [NSString stringWithFormat:@"%.2f元",[price floatValue]];
    
    //      影院距离
    
    int x = arc4random() % 50;
    
    self.distance.text = [NSString stringWithFormat:@"%dkm",x];// _cinemaModel.distance;
    
    //      团，座图标
    

    if (_cinemaModel.isSeatSupport) {
        self.seatMark.image = [UIImage imageNamed:@"cinemaSeatMark@2x"];
    }
    if (_cinemaModel.isCouponSupport) {
        self.couponMark.image = [UIImage imageNamed:@"cinemaCouponMark@2x"];
    }
    if (_cinemaModel.isGroupBuySupport) {
        self.groupMark.image = [UIImage imageNamed:@"cinemaGrouponMark@2x"];
    }
  
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    // 裁剪titlelabel的宽度
    
    CGSize maxSize = CGSizeMake(270, 30);
    
    CGSize realSize = [self.name.text boundingRectWithSize:maxSize
                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                attributes:@{NSFontAttributeName : [UIFont fontWithName:@"黑体-简" size:20]}
                                                   context:nil].size;
    
    
    
    self.name.frame = CGRectMake(5, 5, realSize.width, 30);
    self.address.frame = CGRectMake(5, 35, 270, 25);
    
    CGFloat spacing = (JWCinemaCellHeight - 20 - 20 - 30 ) / 4;     //间距
    self.grade.frame = CGRectMake(5 + self.name.frame.size.width + spacing, spacing, 50, 20);
    self.lowPrice.frame = CGRectMake(JWScreenWidth - 70 - 25, spacing + 20, 70, 30);
    self.distance.frame = CGRectMake(JWScreenWidth - 50 - 25, spacing * 2 + 50, 50, 20);
    
    self.seatMark.frame = CGRectMake(5, self.frame.size.height - JWMarkWith - 5, JWMarkWith, JWMarkWith);
    self.couponMark.frame = CGRectMake(40, self.frame.size.height - JWMarkWith - 5, JWMarkWith, JWMarkWith);
    self.groupMark.frame = CGRectMake(75, self.frame.size.height - JWMarkWith - 5, JWMarkWith, JWMarkWith);
    
}

+ (instancetype)cinemaCellWithTableView:(UITableView *)table{
    static NSString *ID = @"cinemaCell";
    JWCinemaCell *cell = [table dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    return cell;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
